package com.shanghai.shanghaielectric.UI;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.shanghai.shanghaielectric.Constants;

import java.util.List;

import shanghaielectric_lib.Utils.LogUtil;


/**
 * fragment控件
 */
public class FragmentTabAdapter_Main implements RadioGroup.OnCheckedChangeListener {
    private List<Fragment> fragments; // 一个tab页面对应一个Fragment
    private RadioGroup rgs; // 用于切换tab
    private FragmentActivity fragmentActivity; // Fragment所属的Activity
    private int fragmentContentId; // Activity中所要被替换的区域的id
    private int currentTab; // 当前Tab页面索引
    private SharedPreferences psdPreferences;
    private Context mContext;
    private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // 用于让调用者在切换tab时候增加新的功能
    public int beforeId;
    private RadioButton radio_before;

    public FragmentTabAdapter_Main(FragmentActivity fragmentActivity, List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;
        // 默认显示第一页
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        ft.commit();
        rgs.setOnCheckedChangeListener(this);
    }

    public FragmentTabAdapter_Main(Context context, SharedPreferences mPreferences, FragmentActivity fragmentActivity, List<Fragment> fragments,
                                   int fragmentContentId, RadioGroup rgs) {
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;
        this.mContext = context;
        this.psdPreferences = mPreferences;
        // 默认显示第一页
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        ft.commit();
        rgs.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        for (int i = 0; i < rgs.getChildCount(); i++) {
            if (rgs.getChildAt(i).getId() == checkedId) {
                if (i < 5) {
                    Constants.CURRENT_PRESSED_UNIT = 0;
//设置界面，需要输入密码
                    LogUtil.e("dilog", "" + checkedId + "++++" + i);
                    if (i == 3) {
//                        new Dialog_Password().show(getCurrentFragment().getFragmentManager(), "dilog");
//
//                        if (null != onRgsExtraCheckedChangedListener) {
//                            onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(radioGroup, checkedId, i);
//                        }
                        jumpFragment(radioGroup, checkedId, i);

                    }else {
                        jumpFragment(radioGroup, checkedId, i);
                    }

                } else {
                    Constants.CURRENT_PRESSED_UNIT = i - 4;
                    jumpFragment(radioGroup, checkedId, 5);

                }
            }
        }
    }
//	public void checkBefore() {
//		if (beforeId == 0) {
//			radio_before = (RadioButton) rgs.getChildAt(beforeId).findViewById(R.id.radio_monitor);
//		} else if (beforeId == 1) {
//			radio_before = (RadioButton) rgs.getChildAt(beforeId).findViewById(R.id.radio_data);
//		} else if (beforeId == 2) {
//			radio_before = (RadioButton) rgs.getChildAt(beforeId).findViewById(R.id.radio_event);
//		} else if (beforeId == 3) {
//			radio_before = (RadioButton) rgs.getChildAt(beforeId).findViewById(R.id.radio_debugging);
//		} else if (beforeId == 4) {
//			radio_before = (RadioButton) rgs.getChildAt(beforeId).findViewById(R.id.radio_setting);
//		} else if (beforeId == 5) {
//			radio_before = (RadioButton) rgs.getChildAt(beforeId).findViewById(R.id.radio_unit_);
//		}
//		if (radio_before != null) {
//			radio_before.setChecked(true);
//		}
//
//		LogUtil.e("liudanhua", "=====" + beforeId + "===" + radio_before);
//	}

    public void jumpFragment(RadioGroup radioGroup, int checkedId, int tag) {
        Fragment fragment = fragments.get(tag);
        FragmentTransaction ft = obtainFragmentTransaction(tag);

        getCurrentFragment().onPause(); // 暂停当前tab
        // getCurrentFragment().onStop(); // 暂停当前tab

        if (fragment.isAdded()) {
            // fragment.onStart(); // 启动目标tab的onStart()
            fragment.onResume(); // 启动目标tab的onResume()
        } else {
            ft.add(fragmentContentId, fragment);
        }
        showTab(tag); // 显示目标tab
        ft.commit();
        // 如果设置了切换tab额外功能功能接口
        if (null != onRgsExtraCheckedChangedListener) {
            onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(radioGroup, checkedId, tag);
        }

    }

    /**
     * 切换tab
     *
     * @param idx
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);

            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    /**
     * 获取一个带动画的FragmentTransaction
     *
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        // 设置切换动画
//		if (index > currentTab) {
//			ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
//		} else {
//			ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
//		}
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
        return onRgsExtraCheckedChangedListener;
    }

    public void setOnRgsExtraCheckedChangedListener(OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
        this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
    }

    /**
     * 切换tab额外功能功能接口
     */
    public static class OnRgsExtraCheckedChangedListener {
        public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

        }
    }

//    public void showPsdDialog(final RadioGroup radioGroup, final int checkedId, final int dataType) {
//
//        Window windowPop = null;
//        final Dialog dialogPop = new Dialog(mContext, R.style.CustomDialogStyle);
//        dialogPop.show();
//        dialogPop.setCanceledOnTouchOutside(false);
//        windowPop = dialogPop.getWindow();
//        windowPop.setContentView(R.layout.dialog_psd);
//        // 第一个界面
//        final RelativeLayout psddialog_lv1 = (RelativeLayout) windowPop.findViewById(R.id.psddialog_lv1);
//        final EditText psdet = (EditText) windowPop.findViewById(R.id.psdet);
//        TextView forgetpsd = (TextView) windowPop.findViewById(R.id.forgetpsd);
//        Button dialog_sure1 = (Button) windowPop.findViewById(R.id.dialog_sure1);
//        Button dialog_cancle1 = (Button) windowPop.findViewById(R.id.dialog_cancle1);
//        // 第二个界面
//        final RelativeLayout psddialog_lv2 = (RelativeLayout) windowPop.findViewById(R.id.psddialog_lv2);
//        final EditText oldpsdet = (EditText) windowPop.findViewById(R.id.oldpsdet);
//        Button dialog_sure2 = (Button) windowPop.findViewById(R.id.dialog_sure2);
//        Button dialog_cancle2 = (Button) windowPop.findViewById(R.id.dialog_cancle2);
//        // 第三个界面
//        final RelativeLayout psddialog_lv3 = (RelativeLayout) windowPop.findViewById(R.id.psddialog_lv3);
//        final EditText newpsdet = (EditText) windowPop.findViewById(R.id.newpsdet);
//        final EditText newpsdet2 = (EditText) windowPop.findViewById(R.id.newpsdet2);
//        Button dialog_sure3 = (Button) windowPop.findViewById(R.id.dialog_sure3);
//        Button dialog_cancle3 = (Button) windowPop.findViewById(R.id.dialog_cancle3);

//        // 第一个界面的点击事件
//        psddialog_lv1.setVisibility(View.VISIBLE);
//        psddialog_lv2.setVisibility(View.GONE);
//        psddialog_lv3.setVisibility(View.GONE);
//        dialog_sure1.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
//        dialog_cancle1.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mContext != null) {
////					Constants.hideInput(mContext, psdet);
//                }
//
//                dialogPop.cancel();
////                RadioButton radio_monitor = (RadioButton) rgs.getChildAt(0).findViewById(R.id.radio_monitor);
////                radio_monitor.setChecked(true);
//
//            }
//        });
//        forgetpsd.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                psdet.setText("");
//                oldpsdet.setText("");
//                newpsdet.setText("");
//                newpsdet2.setText("");
//                psddialog_lv2.setVisibility(View.VISIBLE);
//                psddialog_lv1.setVisibility(View.GONE);
//                psddialog_lv3.setVisibility(View.GONE);
//            }
//        });
//        // 第二个界面的点击事件
//        dialog_sure2.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String oldpsdetString = oldpsdet.getText().toString().trim();
//                if (oldpsdetString != null && !oldpsdetString.equals("")) {
//                    if (oldpsdetString.equals(Constants.ORIGINALPSD)) {
//                        psdet.setText("");
//                        oldpsdet.setText("");
//                        newpsdet.setText("");
//                        newpsdet2.setText("");
//                        psddialog_lv3.setVisibility(View.VISIBLE);
//                        psddialog_lv1.setVisibility(View.GONE);
//                        psddialog_lv2.setVisibility(View.GONE);
//                    } else {
////						DiaLogUtil.showToast(mContext, mContext.getString(R.string.psd_dialog_str10));
//                    }
//                } else {
////					DiaLogUtil.showToast(mContext, mContext.getString(R.string.psd_dialog_str4));
//                }
//            }
//        });

//        dialog_cancle2.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                psdet.setText("");
//                oldpsdet.setText("");
//                newpsdet.setText("");
//                newpsdet2.setText("");
//                psddialog_lv1.setVisibility(View.VISIBLE);
//                psddialog_lv2.setVisibility(View.GONE);
//                psddialog_lv3.setVisibility(View.GONE);
//            }
//        });

//        // 第三个界面的点击事件
//        dialog_sure3.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                String newpsdetString = newpsdet.getText().toString().trim();
//                String newpsdetString2 = newpsdet2.getText().toString().trim();
//                if (newpsdetString != null && !newpsdetString.equals("")) {
//                    if (newpsdetString.length() > 5) {
//                        if (newpsdetString.equals(newpsdetString2)) {
//                            Editor editor = psdPreferences.edit();// 获取编辑器
//                            editor.putString("psd", newpsdetString);
//                            editor.commit();
//                            Constants.NEWPSD = newpsdetString;
//                            psddialog_lv1.setVisibility(View.VISIBLE);
//                            psddialog_lv2.setVisibility(View.GONE);
//                            psddialog_lv3.setVisibility(View.GONE);
//                            psdet.setText("");
//                            oldpsdet.setText("");
//                            newpsdet.setText("");
//                            newpsdet2.setText("");
//                        } else {
////							DiaLogUtil.showToast(mContext, mContext.getString(R.string.psd_dialog_str13));
//                        }
//                    } else {
////						DiaLogUtil.showToast(mContext, mContext.getString(R.string.psd_dialog_str14));
//                    }
//                } else {
////					DiaLogUtil.showToast(mContext, mContext.getString(R.string.psd_dialog_str12));
//                }
//
//            }
//        });

//        dialog_cancle3.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                psdet.setText("");
//                oldpsdet.setText("");
//                newpsdet.setText("");
//                newpsdet2.setText("");
//                psddialog_lv2.setVisibility(View.VISIBLE);
//                psddialog_lv1.setVisibility(View.GONE);
//                psddialog_lv3.setVisibility(View.GONE);
//            }
//        });

//    }
//
//    public void hidInput(View view) {
//        if (mContext != null) {
//            // 隐藏输入法
//            // InputMethodManager imm = (InputMethodManager)
//            // mContext.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//            // // 显示或者隐藏输入法
//            // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//
//            InputMethodManager imm = (InputMethodManager) mContext.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
//
//        }
//        // if (mContext != null) {
//        // InputMethodManager imm = (InputMethodManager)
//        // mContext.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        // imm.hideSoftInputFromWindow(passwdEdit.getWindowToken(), 0);
//        // }
//
//        // int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
//        // dialogPop.getWindow().addFlags(flags);
//
//    }

}
